package com.liangwj.springbootrabbitmq.rabbit.topic;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <b>Description:</b> TopicSender <br/>
 * @author Lingwenjun
 * @date 2019/1/14-16:22
 * @version 1.0
 */
@Component
public class TopicSender {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public TopicSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }
    /**
     * Topic Exchange
     * 发送send1会匹配到topic.# 和topic.message 两个Receiver都可以收到消息，发送send2只有topic.#可以匹配所有只有Receiver2监听到消息s
     */
    public void send1() {
        String context = "I'm message 1";
        System.out.println("Sender : context = " + context);
        rabbitTemplate.convertAndSend("exchange", "topic.message", context);
    }

    public void send2() {
        String context = "I'm message 2";
        System.out.println("Sender : context = " + context);
        rabbitTemplate.convertAndSend("exchange", "topic.messages", context);
    }

    public void send() {
        String context = "I'm message all";
        System.out.println("Sender : context = " + context);
        rabbitTemplate.convertAndSend("exchange", "topic.all", context);
    }
}
